// pages/user/user-receipt/form/index.js
let App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabled: false,
        error: '',
        items: [{
            name: '普通发票',
            value: '1',
        },
            {
                name: '专用增值税发票',
                value: '2',
                checked: 'true'
            },
        ],
        userReceipt: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 本地获取参数信息
        let that = this
        wx.getStorage({
            key: 'param-userReceiptForm',
            success: function (res) {
                let userReceipt = res.data
                let items = that.data.items
                if (userReceipt == undefined || userReceipt == null || userReceipt.length == 0) {
                    userReceipt = {};
                    userReceipt.receiptType = '2'
                } else if (userReceipt.receiptType == '1') {
                    items = [{
                        name: '普通发票',
                        value: '1',
                        checked: 'true'
                    },
                        {
                            name: '专用增值税发票',
                            value: '2'
                        },
                    ]
                }
                that.setData({
                    userReceipt: userReceipt,
                    items: items
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    /**
     * 表单提交
     */
    saveData: function (e) {
        let _this = this,
            values = e.detail.value

        // 记录formId
        // App.saveFormId(e.detail.formId);

        // 表单验证
        if (!_this.validation(values)) {
            App.showError(_this.data.error);
            return false;
        }

        // 按钮禁用
        _this.setData({
            disabled: true
        });
        let userReceipt = this.data.userReceipt;
        userReceipt.receiptTitle = values.receiptTitle
        userReceipt.taxpayerId = values.taxpayerId
        userReceipt.address = values.address
        userReceipt.telephone = values.telephone
        userReceipt.accountBank = values.accountBank
        userReceipt.accountCode = values.accountCode
        userReceipt.remark = values.remark
        // 提交到后端
        App.api.userReceiptSave(userReceipt).then(res => {
            if (res.data) {
                App.showSuccess(res.msg, function () {
                    wx.navigateBack();
                });
            } else {
                App.showError(res.msg);
            }
            // 解除禁用
            _this.setData({
                disabled: false
            });
        });
    },
    /**
     * 表单验证
     */
    validation: function (values) {
        if (values.receiptType === '') {
            this.data.error = '发票类型不能为空';
            return false;
        }
        if (values.receiptTitle === '') {
            this.data.error = '公司全称不能为空';
            return false;
        }
        if (values.taxpayerId === '') {
            this.data.error = '纳税人识别码不能为空';
            return false;
        }
        if (values.receiptType == '2') {
            if (values.address === '') {
                this.data.error = '注册地址不能为空';
                return false;
            }

            if (values.telephone === '') {
                this.data.error = '注册电话不能为空';
                return false;
            }
            if (values.accounBank === '') {
                this.data.error = '开户银行不能为空';
                return false;
            }
            if (values.accountCode === '') {
                this.data.error = '银行账号不能为空';
                return false;
            }
        }
        return true;
    },
    radioChange: function (e) {
        this.setData({
            [`userReceipt.receiptType`]: e.detail.value
        })
        console.log('radio发生change事件，携带value值为：', e.detail.value)
    },
    isDefaultChange(e) {
        if (e.detail.value) {
            this.setData({
                [`userReceipt.isDefault`]: '1'
            })
        } else {
            this.setData({
                [`userReceipt.isDefault`]: '0'
            })
        }
    },
})